package com.wandian.notepad.mvvm.notepaddetail;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.dialog.CommonDialogTextChoose;
import com.mylib.libcore.dialog.ShareDialogBottom;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.tools.ShareTools;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.AppUtil;
import com.mylib.libcore.utils.DateUtils;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.QRCodeUtil;
import com.mylib.libcore.utils.TimeUtils;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.ViewUtils;
import com.wandian.notepad.R;
import com.wandian.notepad.bean.NoteBean;
import com.wandian.notepad.databinding.NotepadActivityDetailBinding;

import q.rorbin.verticaltablayout.util.DisplayUtil;

import static com.mylib.libcore.config.AppSPKeyConfig.INVITE_CODE_RUL;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 记事本详情天activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NOTEPAD_DETAIL)
public class NotepadDetailActivity extends BaseMVVMActivity<NotepadDetailViewModel> implements
        ShareDialogBottom.CommonDialogBtnClink ,CommonDialogTextChoose.CommonDialogTextBtnClink {
    private NotepadActivityDetailBinding dataBinding;
    private NoteBean noteBean;
    //分享弹框
    private ShareDialogBottom shareDialogBottom;
    private CommonDialogTextChoose cancelDialog;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.notepad_activity_detail);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initClink();
    }


    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindSysTwoToolBar.toolBarTvTitle.setText("记事详情");
        String  notepad_item = getIntent().getStringExtra("notepad_item");
        if(!UIUtils.isEmpty(notepad_item)){
            noteBean = (NoteBean) JsonUtil.parseJsonToBean(notepad_item,NoteBean.class);
        }
        if(!UIUtils.isEmpty(noteBean)){
            long lDay = 0;
            try {
                lDay = DateUtils.getTimeExpend(TimeUtils.timeToMiao(UIUtils.showDataByString(noteBean.getSetTime())), System.currentTimeMillis());
             }catch (Exception e){}
            if(lDay>0){
                dataBinding.tvTag2.setText("还有");
                dataBinding.tvTag2.setTextColor(getResources().getColor(R.color.text_color_blue));
            } else {
                dataBinding.tvTag2.setText("已经");
                dataBinding.tvTag2.setTextColor(getResources().getColor(R.color.sys_text_color));
            }
            dataBinding.tvDayNumber.setText(lDay+"");
            dataBinding.tvNotepadTitle.setText(noteBean.getTitle()+"");
            dataBinding.tvNotepadContent.setText(noteBean.getContent()+"");
            dataBinding.tvAgreedTime.setText("约定时间："+noteBean.getSetTime());
            if(!UIUtils.isEmpty(noteBean.getRingTime())){
                dataBinding.tvWeakTime.setVisibility(View.VISIBLE);
                dataBinding.tvWeakTime.setText("提醒时间："+noteBean.getRingTime()+"");
            }else {
                dataBinding.tvWeakTime.setVisibility(View.GONE);
            }
        }


    }



    private void initClink(){
        dataBinding.bindSysTwoToolBar.toolBarIvBack.setOnClickListener(v -> finish());
        //删除
        dataBinding.llDel.setOnClickListener(v -> {
            if(UIUtils.isFastClick())return;
            if(!UIUtils.isEmpty(noteBean)) showDeleteDialog("是否删除该条记事","",noteBean.getId()+"");
        });
        //分享
        dataBinding.llShare.setOnClickListener(v -> {
            if(UIUtils.isFastClick())return;
            showShareDialog();
        });
    }

    //展示分享弹框
    private void showShareDialog(){
        try {
            shareDialogBottom = new ShareDialogBottom(getContext(),this::btnClink);
            shareDialogBottom.showAtLocation(dataBinding.llRoot, Gravity.BOTTOM, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //展示弹框
    private void showDeleteDialog(String title,String type,String id){
        try {
            cancelDialog = new CommonDialogTextChoose(this,title,type,id,this::sureClink);
            cancelDialog.showAtLocation(dataBinding.llRoot, Gravity.CENTER, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void btnClink(String text, String type, String id) {
        String invite_url = AppContext.getInstance().getAppPreferenceForever().getKeyValue(INVITE_CODE_RUL)+"";
        if(!UIUtils.isEmpty(invite_url)){
            // Bitmap logoBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.app_logo);
            final Bitmap er_bitmap = QRCodeUtil.createQRImage(invite_url, 110, 110, null, null);
            if(!UIUtils.isEmpty(er_bitmap)){
                dataBinding.ivErCode.setImageBitmap(er_bitmap);
            }
        }
        Bitmap share_bitmap = ViewUtils.toBitmap(dataBinding.llNotepadTop);
        if(!UIUtils.isEmpty(share_bitmap)){
            dataBinding.ivErCode.setImageResource(R.color.transparent);
            switch (type){
                case "微信":
                    ShareTools.shareWxPic(getContext(),share_bitmap,0);// 0：分享到好友  1：分享到朋友圈
                    break;
                case "朋友圈":
                    ShareTools.shareWxPic(getContext(),share_bitmap,1);// 0：分享到好友  1：分享到朋友圈
                    break;
                case "QQ":
                    ToastUtil.show("分享QQ");
                    break;
                case "复制":
                    ToastUtil.show("已复制");
                    break;
            }

        }
    }

    @Override
    public void sureClink(String text, String type, String id) {
        mViewModel.deleteItem(id);
    }
}
